package com.itheima.redbaby.activity;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itheima.redbaby.BaseActivity;
import com.itheima.redbaby.R;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.utils.PromptManager;
import com.itheima.redbaby.utils.SoftMap;

public class ProductBigPicActivity extends BaseActivity implements OnClickListener{
	private List<ImageView> imageViewList;
	private ViewPager viewpager;
	private ImageView picView;
	private SoftMap<String, Bitmap> bitmaps = new SoftMap<String, Bitmap>();
	private TextView textProductName;
	private TextView textProductPrice;
	private Product product;
	private List<String> pics;
	private int currPosition;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.prod_bigimg_activity);
		
		product = (Product) getIntent().getSerializableExtra("product");
		currPosition = getIntent().getIntExtra("position", 0);
		pics = product.getBigpic();
		if(currPosition > pics.size() - 1) {
			currPosition = pics.size() - 1;
		}
		
		init();
		getData();
	}
	
	private void init() {
		textProductName = (TextView) findViewById(R.id.textProductName);
		textProductPrice = (TextView) findViewById(R.id.textProductPrice);
		TextView titleBack = (TextView) findViewById(R.id.titleBack);
		RelativeLayout btnLeftArrow = (RelativeLayout) findViewById(R.id.btnLeftArrow);
		RelativeLayout btnRightArrow = (RelativeLayout) findViewById(R.id.btnRightArrow);
		
		viewpager = (ViewPager) findViewById(R.id.productBigPicPager);
		
		//将所有大图放到listview中
		imageViewList = new ArrayList<ImageView>();
		for (int i = 0; i < pics.size(); i++) {
			picView = new ImageView(this);
			imageViewList.add(picView);
		}
		
		titleBack.setOnClickListener(this);
		btnLeftArrow.setOnClickListener(this);
		btnRightArrow.setOnClickListener(this);
		
		BigPicAdapter adapter = new BigPicAdapter();
		viewpager.setAdapter(adapter);
		viewpager.setCurrentItem(currPosition, true);
//		viewpager.scrollBy(0, 0);
		viewpager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageScrollStateChanged(int position) {
			}
		});
	}
	
	private void getData() {
		textProductName.setText(product.getName());
		textProductPrice.setText("￥ " + product.getPrice());
	}
	
	private class BigPicAdapter extends PagerAdapter{
		@Override
		public int getCount() {
			return pics.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(imageViewList.get(position % imageViewList.size()));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			final ImageView view = imageViewList.get(position % imageViewList.size());
			
			final String imgurl = pics.get(position % pics.size());
			if(bitmaps.containsKey(imgurl)){
				view.setImageBitmap(bitmaps.get(imgurl));
			} else {
				new MyHttpTask() {
					@Override
					protected Object doInBackground(Void... params) {
						try {
							URL rl = new URL(imgurl);
							InputStream is = rl.openStream();
							Bitmap bmp = BitmapFactory.decodeStream(is);
							return bmp;
						} catch (Exception e) {
							e.printStackTrace();
						}
						return null;
					}
					@Override
					protected void onPostExecute(Object result) {
						if(result != null) {
							Bitmap bmp = (Bitmap) result;
							view.setImageBitmap(bmp);
							bitmaps.put(imgurl, bmp);
						}
					};
				}.executeHttp();
			}

			container.addView(view);
			return imageViewList.get(position % imageViewList.size());
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.titleBack:
			goBack();
			break;
		case R.id.btnLeftArrow:
			if(currPosition != 0) {
				currPosition--;
				viewpager.setCurrentItem(currPosition, true);
			} else {
				PromptManager.showToast(this, "已经是第一张图片了！");
			}
			break;
		case R.id.btnRightArrow:
			if(currPosition != pics.size() - 1) {
				currPosition++;
				viewpager.setCurrentItem(currPosition, true);
			} else {
				PromptManager.showToast(this, "已经是最后一张图片了！");
			}
			break;
		}
	}

	private void goBack(){
		Intent data = new Intent();
		data.putExtra("position", currPosition);
		setResult(RESULT_OK, data);
		finish();
	}
}

